package cn.ltgodm.template.web.config;

import io.swagger.v3.oas.annotations.OpenAPIDefinition;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.info.Contact;
import io.swagger.v3.oas.annotations.info.Info;
import io.swagger.v3.oas.annotations.info.License;
import io.swagger.v3.oas.annotations.servers.Server;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springdoc.core.GroupedOpenApi;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpHeaders;

/**
 * @author ltgodm
 * @date 2024-03-22 11:08:05
 * swagger文档配置文件
 */
@Configuration
@OpenAPIDefinition(
        // openapi定义描述
        info = @Info(
                title = "${spring.application.name}",
                version = "1.0.0",
                description = "这是一个后台管理系统的模板",
                termsOfService="termsOfService",
                license = @License(name = "Apache 2.0", url = "http://www.apache.org/licenses/LICENSE-2.0.html"),
                contact = @Contact(name = "godm",url = "www.ltgodm.cn",email = "13378920675@163.com")
        ),
        // 请求服务地址配置，可以按不同的环境配置
        servers = {
                @Server(
                        url = "http://localhost:8211",
                        description = "本地地址"
                ),
                @Server(
                        url = "http://dev1-api.kevin.com",
                        description = "dev1环境地址"
                ),
                @Server(
                        url = "http://test1-api.kevin.com",
                        description = "test1环境地址"
                ),
                @Server(
                        url = "http://api.kevin.com",
                        description = "生产环境地址"
                )
        }
)
public class SwaggerConfig {

//    @Bean
//    public OpenAPI customOpenApi(){
//        return new OpenAPI()
//                .info(new Info()
//                        .title("后台管理系统模板接口")
//                        .version("1.0")
//                        .description("后台管理系统模板")
//                        .license(new License().name("Apache 2.0").url("https://springdoc.org"))
//                );
//    }

    @Bean
    public GroupedOpenApi userApi(){
        String[] paths = {"/user/**"};
        String[] packagedToMath = {"cn.ltgodm.template.web.controller.user"};
        return GroupedOpenApi.builder()
                .group("用户模块")
                .pathsToMatch(paths)
                .packagesToScan(packagedToMath)
                .addOpenApiMethodFilter(it -> it.getAnnotation(Operation.class)!=null)
                .build();
    }

    @Bean
    public GroupedOpenApi authApi(){
        String[] paths = {"/auth/**"};
        String[] packagedToMath = {"cn.ltgodm.template.web.controller.auth"};
        return GroupedOpenApi.builder()
                .group("认证模块")
                .pathsToMatch(paths)
                .packagesToScan(packagedToMath)
                .addOpenApiMethodFilter(it -> it.getAnnotation(Operation.class)!=null)
                .build();
    }


}
